package com.wzcl.app.controller;

import com.wzcl.app.dao.model.Channel;
import com.wzcl.app.dao.model.LiveRoom;
import com.wzcl.app.model.common.MyPage;
import com.wzcl.app.model.common.RVO;
import com.wzcl.app.model.dto.LiveRoomDto;
import com.wzcl.app.model.vo.ChannelVo;
import com.wzcl.app.model.vo.LiveRoomVo;
import com.wzcl.app.service.LiveRoomService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;


@Log4j2
@RestController
@RequestMapping("/liveRoom")
@Api(value = "直播管理controller", tags = "直播管理接口")
public class LiveRoomController{

    @Autowired
    private LiveRoomService service;

    @ApiOperation("创建直播")
    @PostMapping("/add")
    public RVO<LiveRoomVo> addLiveRoom(@RequestBody @Valid LiveRoomDto liveRoomDto){
        LiveRoom liveRoom = service.addLiveRoom(liveRoomDto);
        return RVO.success(LiveRoomVo.create(liveRoom));
    }

    @ApiOperation("删除直播间")
    @PostMapping("/del")
    public RVO<LiveRoomVo> delLiveRoom(@RequestBody LiveRoomDto liveRoomDto){
        LiveRoom liveRoom = service.delLiveRoom(liveRoomDto.getNub());
        return RVO.success(LiveRoomVo.create(liveRoom));
    }

    @ApiOperation("修改直播间名字")
    @PostMapping("/updateName")
    public RVO<LiveRoomVo> updateLiveRoomName(@RequestBody LiveRoomDto liveRoomDto){
        LiveRoom liveRoom = service.updateLiveRoomName(liveRoomDto);
        return RVO.success(LiveRoomVo.create(liveRoom));
    }

    @ApiOperation("修改直播间时间")
    @PostMapping("/updateTime")
    public RVO<LiveRoomVo> updateLiveRoomTime(@RequestBody LiveRoomDto liveRoomDto){
        LiveRoom liveRoom = service.updateLiveRoomTime(liveRoomDto);
        return RVO.success(LiveRoomVo.create(liveRoom));
    }

    @ApiOperation("修改直播间老师")
    @PostMapping("/setTeacher")
    public RVO<LiveRoomVo> setTeacherLiveRoom(@RequestBody LiveRoomDto liveRoomDto){
        LiveRoom liveRoom = service.setTeacher(liveRoomDto);
        return RVO.success(LiveRoomVo.create(liveRoom));
    }

    @ApiOperation("去直播")
    @PostMapping("/goLive")
    public RVO<ChannelVo> goLiveRoom(@RequestBody LiveRoomDto liveRoomDto){
        liveRoomDto.setUserPhone(SecurityContextHolder.getContext().getAuthentication().getName());
        Channel channel = service.goLive(liveRoomDto);
        return RVO.success(ChannelVo.create(channel));
    }

    @ApiOperation("看直播")
    @PostMapping("/joinLive")
    public RVO<String> joinLiveRoom(@RequestBody LiveRoomDto liveRoomDto){
        liveRoomDto.setUserPhone(SecurityContextHolder.getContext().getAuthentication().getName());
        String playUrl = service.joinLive(liveRoomDto);
        return RVO.success(playUrl);
    }

    @ApiOperation("看回放")
    @PostMapping("/playBack")
    public RVO<String> getPlayBack(@RequestBody LiveRoomDto liveRoomDto){
        liveRoomDto.setUserPhone(SecurityContextHolder.getContext().getAuthentication().getName());
        String json = service.getPlayBack(liveRoomDto);
        return RVO.success(json);
    }

    @ApiOperation("查找直播间List")
    @PostMapping("/getList")
    public RVO<List<LiveRoomVo>> getLiveRoomList(@RequestBody LiveRoomDto liveRoomDto){
        List<LiveRoom> list = service.getLiveRoomList(liveRoomDto);
        if(list != null && list.size()>0){
            List<LiveRoomVo> collect = list.stream().map(LiveRoomVo::create).collect(Collectors.toList());
            return RVO.success(collect);
        }
        return RVO.success(null);
    }


    @ApiOperation("查找直播间Page")
    @PostMapping("/getPage")
    public RVO<List<LiveRoomVo>> getLiveRoomPage(@RequestBody LiveRoomDto liveRoomDto){
        MyPage<LiveRoom> page = service.getLiveRoomPage(liveRoomDto);
        if(page != null && page.getContent().size()>0){
            List<LiveRoomVo> collect = page.getContent().stream().map(LiveRoomVo::create).collect(Collectors.toList());
            return RVO.success(collect,page);
        }
        return RVO.success(null);
    }
}
